from business.business_base import BusinessBase


class PreUpgradeBusiness(BusinessBase):
    """
    升级前预置业务
    """

    def __init__(self, **kwargs):
        super(PreUpgradeBusiness, self).__init__(**kwargs)
        self.db_b_name_pre = 'db_testb_pre'
        self.up_user_prefix = 'upgrade_'

    def pre_run_compatibility_b(self):
        """
        执行B库相关的业务:建B库，B库下dml
        :return: 无
        """
        if int(self.up_db_v[0].replace('.', '')) != 300:
            self.log.info('升级前-执行B库相关的业务')
            self.check_db_compatibility_b()

    def pre_run_tpcc(self):
        """
        执行tpcc业务
        :return:无
        """
        self.log.info('升级前-执行tpcc业务')
        self.set_tpcc_props()
        self.run_tpcc('build')
        self.run_tpcc('run')

    def pre_run_row_compress(self):
        """
        执行行存压缩、分区表相关业务
        :return:无
        """
        if int(self.up_db_v[0].replace('.', '')) >= 300:
            self.log.info('升级前-执行行存压缩、分区表相关业务')
            self.create_row_compress()
            self.row_compress_dml()

    def pre_check_index(self):
        """
        创建/修改/删除索引及分区表全局索引和分区索引业务
        :return: 无
        """
        self.log.info('升级前-执行索引相关业务')
        self.index()

    def pre_run_logical_replication(self):
        """
        执行逻辑复制相关业务
        :return: 返回逻辑解码结果
        """
        self.log.info('升级前-执行逻辑复制相关业务')
        res = self.logical_replication()
        return res

    def pre_upgrade_business_main(self):
        """
        执行前置业务
        :return: 返回前置逻辑解码业务结果，用作后置中对比
        """
        self.init_business_db_data(self.up_user_prefix, self.db_b_name_pre)
        self.pre_run_row_compress()
        self.pre_run_compatibility_b()
        self.pre_run_tpcc()
        self.pre_check_index()
        res = self.pre_run_logical_replication()
        return res

